package join;

class MyThread extends Thread{
    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        for (int i=1;i<=10;i++){
            System.out.println(getName()+"正在执行!"+i+"次");
        }
    }
}
public class JoinDemo {
    public static void main(String[] args) {
        MyThread mt=new MyThread("子线程");
        mt.start();
        //不加参数:join方法,该线程优先执行(该线程执行结束才会执行其他线程)
        //加毫秒参数:等待该线程终止的最长时间(10毫秒之后无论该线程是否执行结束都会执行其他线程)
        try {
            mt.join(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i=1;i<=10;i++){
            System.out.println("主线程运行"+i+"次!");
        }
        System.out.println("主线程运行结束!");
    }
}
